package cn.hsp.tiktok;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;

import cn.hsp.tiktok.view.ControllerView;
import cn.hsp.tiktok.view.LikeView;

public class VideoAdapter extends BaseRvAdapter<VideoBean, VideoAdapter.VideoViewHolder> {

    public VideoAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindData(VideoViewHolder holder, VideoBean videoBean, int position) {
        holder.controllerView.setVideoData(videoBean);
        Glide.with(getContext()).load(videoBean.getCoverUrl()).into(holder.ivCover);
        holder.likeView.setOnLikeListener(() -> {
            if (!videoBean.isLiked()) {  //未点赞，会有点赞效果，否则无
                holder.controllerView.like();
            }
        });
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_video, parent, false);
        return new VideoViewHolder(view);
    }

    public class VideoViewHolder extends BaseRvViewHolder {
        LikeView likeView;
        ControllerView controllerView;
        ImageView ivCover;

        public VideoViewHolder(View itemView) {
            super(itemView);
            likeView = itemView.findViewById(R.id.likeView);
            controllerView = itemView.findViewById(R.id.controllerView);
            ivCover = itemView.findViewById(R.id.coverIv);
        }
    }
}
